<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //1.封装数学对象
        var myMath = {
            PI: 3.14,
            max: function () {
                var max = arguments[0];
                for (i = 1; i < arguments.length; i++) {
                    if (arguments[i] > max) {
                        max = arguments[i]
                    }
                }
                return max;
            },
            min: function () {
                var min = arguments[0];
                for (i = 1; i < arguments.length; i++) {
                    if (arguments[i] < min) {
                        min = arguments[i]
                    }
                }
                return min;
            }
        }
        console.log(myMath.PI);
        console.log("1.最大值为:" + myMath.max(1, 2, 3, 5));
        console.log("1.最小值为" + myMath.min(1, 46, 54, 3));

        //2.数学其他方法
        console.log('2.绝对值:' + Math.abs(-1));
        console.log('2.向下取整:' + Math.floor(3.54))
        console.log('2.向上取整:' + Math.ceil(3.54))
        console.log('2.四舍五入:' + Math.round(3.5))

        //3.随机取数
        function getRandom(max, min) {
            return Math.floor(Math.random() * (max - min + 1)) + min; //此算法能取到1和10
        }
        console.log("3.随机取得的数字为:" + getRandom(1, 10));

        //4.猜数字游戏
        function getRandom(max, min) {
            return Math.floor(Math.random() * (max - min + 1)) + min; //此算法能取到1和10
        }
        var random = getRandom(1, 50);
        flag = false;
        for(var i = 1; i <= 3; i++){
            var num = prompt("请输入一个数字(1~50)");
            if(num > random){
                console.log('您的数字大了!');
            }else if(num < random){
                console.log('您的数字小了!');
            }else{
                console.log('猜数正确!!!');
                flag = true;
                break;
            }
        }
        if(flag != true){
            console.log('你已经输错3次了,请继续努力!!')
        }
    </script>
</head>

<body>

</body>

</html>